import http from '../../utils/http'
// pages/9-shop/9-shop.js

Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataList:[],
        goodsList:[],
        tel:''
    },
    handleNumberjian(evt){
        let index = evt.currentTarget.dataset.index
        let id = evt.currentTarget.dataset.id
        let item = this.data.dataList[index]
        if(item.number > 1){
            http({
                url:`http://localhost:3000/carts/${id}`,
                method:'put',
                data: {
                    "username": item.username,
                    "tel": item.tel,
                    "goodId": item.goodId,
                    "number": item.number - 1,
                    "checked": false,
                    "id":item.id
                }
            })
            this.data.dataList[index].number = this.data.dataList[index].number - 1
        }
        this.setData({
            dataList:this.data.dataList
        })
    },
    handleNumberjia(evt){
        let index = evt.target.dataset.index
        let id = evt.currentTarget.dataset.id
        let item = this.data.dataList[index]
        if(item.number < 99){
            http({
                url:`http://localhost:3000/carts/${id}`,
                method:'put',
                data: {
                    "username": item.username,
                    "tel": item.tel,
                    "goodId": item.goodId,
                    "number": item.number + 1,
                    "checked": false,
                    "id":item.id
                }
            })
            this.data.dataList[index].number = this.data.dataList[index].number + 1
        }
        this.setData({
            dataList:this.data.dataList
        })
    },
    checkchange(evt){
        let index = evt.target.dataset.index
        this.data.dataList[index].checked=!this.data.dataList[index].checked
        this.setData({
            dataList:this.data.dataList
        })
    },
    delete(){
        var deleteArr
        deleteArr = this.data.dataList.filter(item=>{
            return item.checked
        })
        deleteArr.forEach(item=>{
            http({
                url:`http://localhost:3000/carts/${item.id}`,
                method:"delete"
            })
        })
        this.data.dataList = this.data.dataList.filter(item=>{
            return !item.checked
        })
        this.setData({
            dataList:this.data.dataList
        })
    },
    goshop(evt){
        let total = evt.target.dataset.total
        this.delete()
        wx.showToast({
          title: `结账成功`,
        })
        console.log()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var tel = wx.getStorageSync('tel')
        this.setData({
            tel:tel
        })
        this.updataDataList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var tel = wx.getStorageSync('tel')
        this.setData({
            tel:tel
        })
        this.updataDataList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    updataDataList(){
        http({
            url:`http://localhost:3000/carts?tel=${this.data.tel}`,
            method:'get'
        }).then(res=>{
            // console.log(res.data)
            this.setData({
                dataList:res.data,
            })
            //将物品数据请求回来
            Promise.all(res.data.map(item=>{
                return http({
                    url:`http://localhost:3000/goods?id=${item.goodId}`,
                    method:'get'
                })
            })).then(res=>{
                var arr = []
                for(let i = 0;i<res.length;i++){
                    this.data.dataList[i].title = res[i].data[0].title
                    this.data.dataList[i].price = res[i].data[0].price
                    this.data.dataList[i].poster = res[i].data[0].poster
                    this.data.dataList[i].feature = res[i].data[0].feature
                }
                this.setData({
                    dataList:this.data.dataList
                })
                // console.log(this.data.dataList)
            })
        })
    },
    handleToLogin(){
        wx.switchTab({
          url: '/pages/center/center',
        })
    }
})